package main

import (
	"bufio"
	"io"
	"log"
	"os"
	"unicode"
)

// (比如netty官网的例子)解决每行是行号加代码
// 执行后导入IDE(如eclipse, idea), 格式化一下代码

// depends golang; usage: go run remove_num.go file1 file2...
func main() {
	file := os.Args[1:]
	for _, fileName := range file {
		f, err := os.Open(fileName)
		if err != nil {
			log.Fatal(err)
		}
		// 以源文件文件名 + ".bak" 为新文件名
		newFileName := f.Name() + ".bak"
		newFile, err := os.Create(newFileName)
		if err != nil {
			log.Fatal(err)
		}
		defer newFile.Close()
		defer f.Close()

		rd := bufio.NewReader(f)
		for {
			var index int
			line, err := rd.ReadString('\n')
			for k, v := range line {
				if !unicode.IsNumber(v) {
					index = k
					break
				}
			}
			newLine := line[index:]
			newFile.WriteString(newLine)
			if err != nil || io.EOF == err {
				break
			}
		}
	}
}
